<template>
  <div>
    <el-tree
      ref="treeRef"
      :data="list"
      :props="{label:'name'}"
      :show-checkbox="true"
      :default-expand-all="true"
      :check-strictly="true"
      node-key="id"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="doSave">确 定</el-button>
      <el-button @click="doCancle">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permissions'
import { arrayToTree } from '@/utils/arrayToTree'
import { getRoleDetail, assignPerm } from '@/api/settings'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
    this.loadPermissionDetail()
  },
  methods: {
    // 加载权限列表
    async loadPermissionList() {
      const { data: res } = await getPermissionList()
      console.log('权限信息', res)
      this.list = arrayToTree(res.data, '0')
    },
    // 权限详情
    async loadPermissionDetail() {
      const { data: res } = await getRoleDetail(this.curId)
      //   console.log('res详情', res)
      // 数据回填
      this.$refs.treeRef.setCheckedKeys(res.data.permIds)
    },
    // 更新权限
    async doSave() {
      const permIds = this.$refs.treeRef.getCheckedKeys()
      const { data: res } = await assignPerm({ id: this.curId, permIds: permIds })
      console.log('更新分配权限', res)
      // 提示信息
      this.$message.success(res.message)
      // 通知父组件关闭弹层
      this.$emit('success')
    },
    // 取消
    doCancle() {
      this.$emit('close')
    }
  }
}
</script>

<style>

</style>
